%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT TITLE : OMR-Staff Parameters
%
% EXPLANATION    : This function finds staff space height and staff line
% height calculating the most frequent white and black vertical runs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [staffspace_height , staffline_height] = find_staff_parameters(sheet)

% start initializations
[m n]=size(sheet); % find size of the image

black_run=zeros(1,n*ceil(m/2));
white_run=zeros(1,n*ceil(m/2));
w_ind=1;
b_ind=1;
% end of initializations

% start calculation of black and white vertical runs
for i=1:n
    counter=1;
    for j=2:m
        if sheet(j,i)==sheet(j-1,i)
            counter=counter+1;
        else
            if sheet(j-1,i)==0
                black_run(b_ind)=counter;
                b_ind=b_ind+1;
                counter=1;
            else
                white_run(w_ind)=counter;
                w_ind=w_ind+1;
                counter=1;
            end
        end
    end
    
    if sheet(j,i)==0
        black_run(b_ind)=counter;
        b_ind=b_ind+1;
        counter=1;
    else
        white_run(w_ind)=counter;
        w_ind=w_ind+1;
        counter=1;
    end
end
% end of calculation of black and white vertical runs

whites=white_run(find(white_run)); % eliminate zeros
blacks=black_run(find(black_run)); % eliminate zeros
staffspace_height=mode(whites); % find the most frequent white value
staffline_height=mode(blacks); % find the most frequent black values